###################################
## hate crime analysis code
## 2024.12.1
###################################

library(dplyr)
library(ggplot2)
library(Rmisc)
library(haven)
library(rlang)
library(purrr)
library(broom)
library(xtable)

####################################################
## Load Data                                     
####################################################

# load data
df_all <- readRDS("hatecrime_replication.Rds")


####################################################
## Functions for figures
####################################################

summarize_by_treatment <- function(df, measurevar) {
  summarySE(df[!is.na(df$crime_treatment), ], measurevar = measurevar, groupvars = "crime_treatment", na.rm = TRUE) %>%
    mutate(point = sprintf(get(measurevar) * 100, fmt = '%#.1f'))
}

generate_plot <- function(data, y_var, y_label, y_start, text_1, text_2, text_3, style = 1) {
  y_var_sym <- sym(y_var)
  interval <- -qnorm((1-0.95)/2)  # 95% multiplier
  treatment_labels <- c("Control", "Hate Crime", "Hate Crime \n + \n Congressional Action")
  
  plot <- ggplot(data, aes(x = crime_treatment, y = !!y_var_sym * 100, fill = crime_treatment)) +
    geom_bar(position = "dodge", stat = "identity", width = 0.45, alpha = 0.9) + 
    theme_minimal() + 
    scale_fill_manual(values = c("#0072B2", "#83000D", "#FF6666"), labels = c("Control Group", "Hate Crime Treatment", "Hate Crime + Congressional Action")) +
    geom_errorbar(aes(ymin = !!y_var_sym * 100 - se * interval * 100, ymax = !!y_var_sym * 100 + se * interval * 100), width = 0.05, position = position_dodge(width = 0.5)) +
    geom_text(aes(label = point), size = 3.5, vjust = -0.5, hjust = -0.4, nudge_x = 0.05) +
    xlab("") +
    ylim(0, 57.5) +
    scale_x_discrete(labels = treatment_labels) +
    theme(axis.text.x = element_text(size = 9.5)) + 
    ylab(y_label) + 
    theme(legend.position = "none") + 
    guides(fill = guide_legend(title = "")) + 
    # difference between control & hate crime treatment 
    geom_segment(aes(x = 1, y = y_start, xend = 1.1, yend = y_start + 1), colour = "darkgray") +
    geom_segment(aes(x = 1.1, y = y_start + 1, xend = 1.9, yend = y_start + 1), colour = "darkgray") +
    geom_segment(aes(x = 1.9, y = y_start + 1, xend = 2, yend = y_start), colour = "darkgray") + 
    annotate("text", x = 1.5, y = y_start + 2.5, label = text_1, size = 3.5, fontface = 2) + 
    # difference between hate crime treatment & policy response treatment
    geom_segment(aes(x = 2, y = y_start, xend = 2.1, yend = y_start + 1), colour = "darkgray") +
    geom_segment(aes(x = 2.1, y = y_start + 1, xend = 2.9, yend = y_start + 1), colour = "darkgray") +
    geom_segment(aes(x = 2.9, y = y_start + 1, xend = 3, yend = y_start), colour = "darkgray") + 
    annotate("text", x = 2.5, y = y_start + 2.5, label = text_2, size = 3.5, fontface = 2)
  
  if (style == 1) {
    plot <- plot +
      geom_segment(aes(x = 1, y = y_start + 3, xend = 1.1, yend = y_start + 4), colour = "darkgray") +
      geom_segment(aes(x = 1.1, y = y_start + 4, xend = 2.9, yend = y_start + 4), colour = "darkgray") +
      geom_segment(aes(x = 2.9, y = y_start + 4, xend = 3, yend = y_start + 3), colour = "darkgray") +
      annotate("text", x = 2, y = y_start + 5.5, label = text_3, size = 3.5, fontface = 2)
  } else if (style == 2) {
    plot <- plot +
      geom_segment(aes(x = 1, y = y_start + 4, xend = 1.1, yend = y_start + 5), colour = "darkgray") +
      geom_segment(aes(x = 1.1, y = y_start + 5, xend = 2.9, yend = y_start + 5), colour = "darkgray") +
      geom_segment(aes(x = 2.9, y = y_start + 5, xend = 3, yend = y_start + 4), colour = "darkgray") +
      annotate("text", x = 2, y = y_start + 6.5, label = text_3, size = 3.5, fontface = 2)
  }
  
  return(plot)
}

#####################################
## Figure 1: General Views on the US
#####################################

# control vs. hate crime 
summary(lm(crime_view_us_binary ~ c_v_hc, data = df_all))
# 10.1%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_us_binary ~ hc_v_pr , data = df_all))
# 3.5%p (p=0.003)

# control vs. congressional action
summary(lm(crime_view_us_binary ~ c_v_pr , data = df_all))
# 6.6%p (p=0.000)

view_us_binary_sum <- summarize_by_treatment(df_all, "crime_view_us_binary")
plot <- generate_plot(view_us_binary_sum, "crime_view_us_binary", "Unfavorable Views of the US(%)", 43, "10.1%p (p=0.000)", "3.5%p (p=0.003)", "6.6%p (p=0.000)", style = 1)
ggsave("figures/Figure1_General.png", plot, width = 5, height = 5, units = "in", dpi = 600)

###############################################################
## Figure 2: Four Negative Attitudinal Variables toward the US
###############################################################

## (a): Confidence in the US

# control vs. hate crime 
summary(lm(crime_confidence_us_binary ~ c_v_hc, data=df_all)) # 6.3%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_confidence_us_binary ~ hc_v_pr , data=df_all)) # 2.3%p (p=0.048)

# control vs. congressional action
summary(lm(crime_confidence_us_binary ~ c_v_pr , data=df_all)) # 4.0%p (p=0.001)

confidence_us_binary_sum <- summarize_by_treatment(df_all, "crime_confidence_us_binary")
plot <- generate_plot(confidence_us_binary_sum, "crime_confidence_us_binary", "No Confidence in the US (%)", 44, "6.3%p (p=0.000)", "2.3%p (p=0.048)", "4.0%p (p=0.001)", style = 1)
ggsave("figures/Figure2a_Confidence.png", plot, width=5, height=5, units = "in", dpi=600)

## (b): Views toward American Ideas and Customs

# control vs. hate crime 
summary(lm(crime_view_idea ~ c_v_hc, data=df_all)) # 11.0%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_idea ~ hc_v_pr , data=df_all)) # 5.7%p (p=0.000)

# control vs. congressional action
summary(lm(crime_view_idea ~ c_v_pr , data=df_all)) # 5.3%p (p=0.000)

view_idea_us_binary_sum <- summarize_by_treatment(df_all, "crime_view_idea")
plot <- generate_plot(view_idea_us_binary_sum, "crime_view_idea", "Unfavorable Views of American Ideas and Customs (%)", 52, "11.0%p (p=0.000)", "5.7%p (p=0.000)", "5.3%p (p=0.000)", style = 1)
ggsave("figures/Figure2b_Idea.png", plot, width=5, height=5, units = "in", dpi=600)

## (c): Views toward American Democracy

# control vs. hate crime 
summary(lm(crime_view_democracy ~ c_v_hc, data=df_all)) # 6.5%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_democracy ~ hc_v_pr , data=df_all)) # 0.3%p (p=0.791)

# control vs. congressional action
summary(lm(crime_view_democracy ~ c_v_pr , data=df_all)) # 6.1%p (p=0.000)

view_democracy_us_binary_sum <- summarize_by_treatment(df_all, "crime_view_democracy")
plot <- generate_plot(view_democracy_us_binary_sum, "crime_view_democracy", "Unfavorable Views of American Democracy (%)", 44, "6.5%p (p=0.000)", "", "6.1%p (p=0.000)", style = 1)
plot <- plot +
  annotate("text", x=2.5, y=46.5, label="0.3%p (p=0.791)", size=3.5, fontface=2, color="darkgray")
ggsave("figures/Figure2c_Democracy.png", plot, width=5, height=5, units = "in", dpi=600)

## (d): Views toward Americans

# control vs. hate crime 
summary(lm(crime_view_americans_binary ~ c_v_hc, data=df_all)) # 11.8%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_americans_binary ~ hc_v_pr , data=df_all)) # 1.9%p (p=0.106)

# control vs. congressional action
summary(lm(crime_view_americans_binary ~ c_v_pr , data=df_all)) # 9.9%p (p=0.000)

view_americans_binary_sum <- summarize_by_treatment(df_all, "crime_view_americans_binary")
plot <- generate_plot(view_americans_binary_sum, "crime_view_americans_binary", "Unfavorable Views of Americans (%)", 43, "11.8%p (p=0.000)", "", "9.9%p (p=0.000)", style = 1)
plot <- plot +
  annotate("text", x=2.5, y=45.5, label="1.9%p (p=0.106)", size=3.5, fontface=2, color="darkgray")
ggsave("figures/Figure2d_Americans.png", plot, width=5, height=5, units = "in", dpi=600)

###############################################################
## Figure 3: Results on the Views toward American Popular Culture
###############################################################

# control vs. hate crime 
summary(lm(crime_view_culture ~ c_v_hc, data=df_all)) # 0.3%p (p=0.746)

# hate crime vs. congressional action
summary(lm(crime_view_culture ~ hc_v_pr , data=df_all)) # 0.0%p (p=0.991)

# control vs. congressional action
summary(lm(crime_view_culture ~ c_v_pr , data=df_all)) # 0.3%p (p=0.738)

view_culture_sum <- summarize_by_treatment(df_all, "crime_view_culture")
plot <- generate_plot(view_culture_sum, "crime_view_culture", "Unfavorable Views of American Pop Culture (%)", 24, "", "", "", style = 1)
plot <- plot +
  annotate("text", x=1.5, y=26.5, label="0.3%p (p=0.746)", size=3.5, color="darkgray") + 
  annotate("text", x=2.5, y=26.5, label="0.0%p (p=0.991)", size=3.5, color="darkgray") + 
  annotate("text", x=2, y=29.5, label="0.3%p (p=0.738)", size=3.5, color="darkgray")

ggsave("figures/Figure3_Culture.png", plot, width=5, height=5, units = "in", dpi=600)

###############################################################
## Figure 4: General Views on the US by Countries
###############################################################

df_chn <- subset(df_all, country_name=="China")
df_idn <- subset(df_all, country_name=="Indonesia")
df_jpn <- subset(df_all, country_name=="Japan")
df_kor <- subset(df_all, country_name=="Korea")
df_mys <- subset(df_all, country_name=="Malaysia")
df_phl <- subset(df_all, country_name=="Philippines")
df_sgp <- subset(df_all, country_name=="Singapore")
df_tha <- subset(df_all, country_name=="Thailand")
df_vnm <- subset(df_all, country_name=="Vietnam")

df_list <- list(df_chn, df_idn, df_jpn, df_kor, df_mys, df_phl, df_sgp, df_tha, df_vnm)

summary_binary <- data.frame()
for(i in 1:length(df_list)) {  

  df <- df_list[[i]]
  df <- subset(df, is.na(mc_skip)==TRUE) 
  
  df$crime_view_us_binary <- ifelse(df$crime_view_us>2, 1, 0)
  
  view_us_sum_binary <- summarySE(df[which(is.na(df$crime_treatment)==FALSE),], measurevar=c("crime_view_us_binary"), groupvars=c("country_name", "crime_treatment"), na.rm=TRUE)
  summary_binary <- rbind(summary_binary, view_us_sum_binary)
  
}

summary_binary$point <- sprintf(summary_binary$crime_view_us_binary*100, fmt = '%#.2f')
interval <- -qnorm((1-0.95)/2)  # 95% multiplier

ggplot(summary_binary, aes(x=factor(country_name), y=100*crime_view_us_binary, fill=crime_treatment)) +
  geom_bar(position="dodge", stat="identity", width=0.9, alpha=0.9) + theme_minimal() + scale_fill_manual(values=c("#0072B2","#83000D", "#FF6666"), labels = c("Control", "Hate Crime", "Hate Crime + Congressional Action")) +
  geom_errorbar(aes(ymin = 100*(crime_view_us_binary - se*interval), ymax = 100*(crime_view_us_binary + se*interval)), width = 0.3, position = position_dodge(width = 0.9)) +
  xlab("") +
  ylab("Unfavorable Views of the US (%)") + 
  theme(legend.position="bottom") + 
  guides(fill = guide_legend(title = ""))

ggsave("figures/Figure4_ByCountry.png", width=7, height=5, units = "in", dpi=600)

#################################################################################################
## Figure 5: Unfavorable Views on the US Across Different Positions on the US-China Competition
#################################################################################################

## (a): Pro-US

df_order_pro_us <- df_all%>%
  filter(term !="(Intercept)") %>%
  filter(view_us_china_order == 1 | view_us_china_order == 2) 

# control vs. hate crime
summary(lm(crime_view_us_binary ~ c_v_hc, data=df_order_pro_us)) # 10.0%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_us_binary ~ hc_v_pr , data=df_order_pro_us)) # 3.0%p (p=0.030)

# control vs. congressional action
summary(lm(crime_view_us_binary ~ c_v_pr , data=df_order_pro_us)) # 7.0%p (p=0.000)

general_pro_sum <- summarize_by_treatment(df_order_pro_us, "crime_view_us_binary")
plot <- generate_plot(general_pro_sum, "crime_view_us_binary", "Unfavorable Views of the US (%)", 27, "10.0%p (p=0.000)", "3.0%p (p=0.030)", "7.0%p (p=0.000)", style = 2)
plot <- plot + ylim(0, 86) + annotate("text", x=0.7, y=83, label="Pro-US", size=4.5, fontface =2)

ggsave("figures/Figure5a_ProUS.png", plot, width=5, height=5, units = "in", dpi=600)

## (b): Pro-China

df_order_anti_us <- df_all%>%
  filter(term !="(Intercept)") %>%
  filter(view_us_china_order == 4 | view_us_china_order == 5) 

# control vs. hate crime 
summary(lm(crime_view_us_binary ~ c_v_hc, data=df_order_anti_us)) # 4.7%p (p=0.053)

# hate crime vs. congressional action
summary(lm(crime_view_us_binary ~ hc_v_pr , data=df_order_anti_us)) # 0.0%p (p=0.975)

# control vs. congressional action
summary(lm(crime_view_us_binary ~ c_v_pr , data=df_order_anti_us)) # 4.6%p (p=0.057)

general_anti_sum <- summarize_by_treatment(df_order_anti_us, "crime_view_us_binary")
plot <- generate_plot(general_anti_sum, "crime_view_us_binary", "Unfavorable Views of the US (%)", 79, "4.7%p (p=0.053)", "", "4.6%p (p=0.057)", style = 2)
plot <- plot + ylim(0, 86) + annotate("text", x=0.7, y=83, label="Pro-China", size=4.5, fontface =2) +
  annotate("text", x=2.5, y=82, label="0.0%p (p=0.975)", size=3.5, color="darkgray", fontface = 2) 

ggsave("figures/Figure5b_ProChina.png", plot, width=5, height=5, units = "in", dpi=600)

## (c): Indifference

df_order_ind_us <- df_all%>%
  filter(term !="(Intercept)") %>%
  filter(view_us_china_order == 3) 

# control vs. hate crime 
summary(lm(crime_view_us_binary ~ c_v_hc, data=df_order_ind_us)) # 15.0%p (p=0.000)

# hate crime vs. congressional action
summary(lm(crime_view_us_binary ~ hc_v_pr , data=df_order_ind_us)) # 7.7%p (p=0.000)

# control vs. congressional action
summary(lm(crime_view_us_binary ~ c_v_pr , data=df_order_ind_us)) # 7.3%p (p=0.000)

general_ind_sum <- summarize_by_treatment(df_order_ind_us, "crime_view_us_binary")
plot <- generate_plot(general_ind_sum, "crime_view_us_binary", "Unfavorable Views of the US (%)", 50, "15.0%p (p=0.000)", "7.7%p (p=0.000)", "7.3%p (p=0.000)", style = 2)
plot <- plot + ylim(0, 86) + annotate("text", x=0.7, y=83, label="Indifferent", size=4.5, fontface =2) 

ggsave("figures/Figure5c_Indifferent.png", plot, width=5, height=5, units = "in", dpi=600)

